#ifndef WAVFILE_H_
#define WAVFILE_H_

#include "Common.h"
#include "shared_ptr.hpp"
#include <string>

// reference: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
class WavFile
{
    private:
        uint32 _chunkID;
        uint32 _chunkSize;
        uint32 _format;

        // Format part
        uint32 _subChunk1ID;
        uint32 _subChunk1Size;
        uint16 _audioFormat;
        uint16 _numChannels;
        uint32 _sampleRate;
        uint32 _byteRate;
        uint16 _blockAlign;
        uint16 _bitsPerSample;

        // Data part
        uint32 _subChunk2ID;
        uint32 _subChunk2Size;

        byte* _data;
        uint32 _dataLen;

        bool _isValidWavFile;
        BYTE_ORDER_T _byteOrder;

        WavFile(const byte* wav, uint32 wavLen, BYTE_ORDER_T byteOrder);

        bool IsValidWavFile() { return _isValidWavFile; }

        BYTE_ORDER_T GetByteOrder() { return _byteOrder; }

        int UnpackSamples(BYTE_ORDER_T byteOrder, int *sample_buffer, int bufSize);

        uint32 GetNumSamples() { return _dataLen / ((_bitsPerSample + 7) / 8); }

        int GetWavSamples(lame_t gfp, int* leftTone, int* rightTone, int lenOfTone);

        void InitLameFlags(lame_global_flags* gf);

    public:
        static WavFile* Create(const byte* wav, uint32 wavLen, BYTE_ORDER_T byteOrder);

        ~WavFile();

        int ToMP3(boost::shared_ptr<Item>&);

        void Print();

        static WavFile* CreateTestWavFile(std::string inPath);

};

#endif /* WAVFILE_H_ */
